package de.hs.rm;

import android.app.Activity;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class BHModeKeyboardActivity extends Activity {
	
	private BHMainApplication mainApp= null;
	private LinearLayout[] keyboardRows = new LinearLayout[6];
	private KeyButton[][] buttons = new KeyButton[6][];
	private KeyboardKey[][] toggleLayouts = {KeyboardKeys.functionKeys,
			KeyboardKeys.punctiationKeys,
			KeyboardKeys.specKeys};
	private int togglePos = 0;
	
	private boolean altGr = false;
	private boolean shift = false;
	private boolean alt = false;
	private boolean ctrl = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Drawable bgImage = getResources().getDrawable(R.drawable.keyboardbackground);
        bgImage.setAlpha(30);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modekeyboard);
        
        mainApp = (BHMainApplication)this.getApplicationContext();
        
        setupKeyboard();
        
    }

	private void setupKeyboard() {
		keyboardRows[0] = (LinearLayout) findViewById(R.id.keyboardrow1);
        keyboardRows[1] = (LinearLayout) findViewById(R.id.keyboardrow2);
        keyboardRows[2] = (LinearLayout) findViewById(R.id.keyboardrow3);
        keyboardRows[3] = (LinearLayout) findViewById(R.id.keyboardrow4);
        keyboardRows[4] = (LinearLayout) findViewById(R.id.keyboardrow5);
        keyboardRows[5] = (LinearLayout) findViewById(R.id.keyboardrow6);
        
        View leftToggle = (View)(keyboardRows[1].getChildAt(0));
        leftToggle.setOnClickListener(new ToggleKeyListener());
        
        int exRows = 0;
        
        for (int i = 0; i < keyboardRows.length; i++)
        {
        	int keyCount = keyboardRows[i].getChildCount();
        	buttons[i] = new KeyButton[keyCount];
        	
        	if (i == 1 || i== 3)
        		exRows = 1;
        	else
        		exRows = 0;
        	for (int j = 0 + exRows; j < keyCount - exRows; j++)
        	{
        		
        		buttons[i][j-exRows] = (KeyButton)(keyboardRows[i].getChildAt(j));
        		if(i < keyboardRows.length -1)
        		{
//        			buttons[i][j].setText(
//        					Html.fromHtml("<small>" + (keys.substring(currKey, currKey + 1)).toLowerCase()+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>" +  "<br />" + 
//        				            "<big><b>" + (keys.substring(currKey, currKey + 1)) + "</b></big>" + 
//        				            "<small>&nbsp;&nbsp;&nbsp;&nbsp;" + "1" + "</small>")
//        					);
        			buttons[i][j-exRows].setKey(KeyboardKeys.germanLayout[i][j-exRows]);
        			//buttons[i][j-exRows].setOnClickListener(new clickKeyListener());
        			buttons[i][j-exRows].setOnTouchListener(new normKeyListener());
        			//currKey++;
        		}
        		else
        		{
        			buttons[i][j].setKey(KeyboardKeys.germanLayout[i][j-exRows]);
        			buttons[i][j].setOnTouchListener(new normKeyListener());
        			//buttons[i][j].setOnClickListener(new clickKeyListener());
        		}
        	}
        }
	}
	
	private class ToggleKeyListener implements OnClickListener
	{
		
		@Override
		public void onClick(View v) {
			togglePos = (togglePos + 1)%3;
			int x = buttons[1].length;
			for(int i = 0; i < buttons[1].length -2; i++)
			{
				
				buttons[1][i].setKey(toggleLayouts[togglePos][i]);
				
			}
			
		}
	
	}
	
	private class clickKeyListener implements OnClickListener
	{

		@Override
		public void onClick(View v) {
			int msg =  ((KeyButton)v).kbKey.keyCode;
			mainApp.sendBTMessage("keyboard:keydown:xA" + msg + "xE");
			
		}
	
	}
	
	private class normKeyListener implements OnTouchListener
	{
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int msg =  ((KeyButton)v).kbKey.keyCode;
//			int action = event.getActionMasked();
//			int pointerIndex = event.getActionIndex();
//		    int pointerId = event.getPointerId(pointerIndex);
//			
//			if ((pointerIndex) == 0 && (pointerId == 0) && event.getPointerCount() == 1)
//			{
			
			
			switch (event.getAction())
	    	{
	    		case MotionEvent.ACTION_DOWN:
	    			switch (msg)
	    			{
	    				case 18://KeyEvent.VK_ALT
	    					toggleModKey(alt, (KeyButton)v);
	    					alt = !alt;
	    					break;
	    				case 65406://KeyEvent.VK_ALT_GRAPH
	    					toggleModKey(altGr, (KeyButton)v);
	    					altGr = !altGr;
	    					break;
	    				case 17: //KeyEvent.VK_CONTOL
	    					toggleModKey(ctrl, (KeyButton)v);
	    					ctrl = !ctrl;
	    					break;
	    				case 16: //KeyEvent.VK_SHIFT
	    					toggleModKey(shift, (KeyButton)v);
	    					shift = !shift;
	    					break;
	    			}
	    			mainApp.sendBTMessage("keyboard:keydown:xA" + msg + "xE");
	    			break;
	    		case MotionEvent.ACTION_UP:
	    			mainApp.sendBTMessage("keyboard:keyup:xA" + msg + "xE");
	    			break;
	    	}
//			}
//			
//			if((pointerIndex == 1) && (pointerId == 1) && (event.getPointerCount() == 2))
//			{
//				switch (action)
//		    	{
//		    		case MotionEvent.ACTION_POINTER_DOWN:
//		    			mainApp.sendBTMessage("keyboard:keydown:xA" + msg + "xE");
//		    			break;
//		    		case MotionEvent.ACTION_POINTER_UP:
//		    			mainApp.sendBTMessage("keyboard:keyup:xA" + msg + "xE");
//		    			break;
//		    	}
//			}
				
			return true;
		}
		
		private void toggleModKey(boolean key, KeyButton kButton)
		{
			if(!key)
				kButton.getBackground().setColorFilter(0xffA4C639, PorterDuff.Mode.MULTIPLY);
			else
				kButton.getBackground().setColorFilter(0xffbbbbbb, PorterDuff.Mode.MULTIPLY);
		}
	}
	
	private class specKeyListener implements OnTouchListener
	{
		@Override
		public boolean onTouch(View v, MotionEvent event) {
//			for (int i = 0; i < buttons.length; i++)
//	        {
//	        	for (int j = 0 ; j < buttons[i].length; j++)
//	        	{
//	        		if(buttons[i][j] != null)
//	        			buttons[i][j].setShift();
//	        	}
//	        }
			int msg =  ((KeyButton)v).kbKey.keyCode;
//			int action = event.getActionMasked();
//			int pointerIndex = event.getActionIndex();
//		    int pointerId = event.getPointerId(pointerIndex);
			
//			if ((pointerIndex) == 0 && (pointerId == 0) && event.getPointerCount() == 1)
//			{
				switch (event.getAction())
		    	{
		    		case MotionEvent.ACTION_DOWN:
		    			mainApp.sendBTMessage("keyboard:keydown:xA" + msg + "xE");
		    			break;
//		    		case MotionEvent.ACTION_UP:
//		    			mainApp.sendBTMessage("keyboard:keyup:xA" + msg + "xE");
//		    			break;
		    	}
//			}
//			
//			if((pointerIndex == 1) && (pointerId == 1) && (event.getPointerCount() == 2))
//			{
//				switch (action)
//		    	{
//		    		case MotionEvent.ACTION_POINTER_DOWN:
//		    			mainApp.sendBTMessage("keyboard:keydown:xA" + msg + "xE");
//		    			break;
//		    		case MotionEvent.ACTION_POINTER_UP:
//		    			mainApp.sendBTMessage("keyboard:keyup:xA" + msg + "xE");
//		    			break;
//		    	}
//			}
				
			return true;
		}
	}
}